package com.walker.service.catalogue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.walker.util.CommonUtils;

import net.sf.json.JSONObject;

@Service
public class CatalogueDataSourceService {

	@Autowired
	@Qualifier("dmJdbcTemplate")
	private JdbcTemplate dmJdbcTemplate;
	
	/**
	 * 数据源列表
	 * @param params
	 * @return
	 */
	public Map<String,Object> queryDataSourceList(Map<String,Object> params){
		String keyword = CommonUtils.valueOf(params.get("keyword"));
		String data_source_type = CommonUtils.valueOf(params.get("data_source_type"));
		String data_source_name = CommonUtils.valueOf(params.get("data_source_name"));
		int pageSize = Integer.parseInt(CommonUtils.valueOf(params.get("pageSize")));
		int pageNo = Integer.parseInt(CommonUtils.valueOf(params.get("pageNo")));
		int offset = (pageNo - 1) * pageSize ;
		
		List<Object> paramList = new ArrayList<Object>();
		
		String countSql = getDataSourceCountSql(paramList,data_source_name,keyword,data_source_type);
		Object totalRecords = dmJdbcTemplate.queryForMap(countSql,paramList.toArray()).get("count");
		
		String sql = getDataSourceSql(data_source_name,keyword,data_source_type);
		paramList.add(pageSize);
		paramList.add(offset);
		List<Map<String,Object>> sourceList = dmJdbcTemplate.queryForList(sql,paramList.toArray());
		
		sql = "select id,param_key,param_value from t_meta_data_source_param";
		List<Map<String, Object>> sourceParams = dmJdbcTemplate.queryForList(sql);
		for (Map<String, Object> source : sourceList) {
			JSONObject json = new JSONObject();
			for (Map<String, Object> sourceParam : sourceParams) {
				if (source.get("id").equals(sourceParam.get("id"))) {
					json.put(sourceParam.get("param_key"), sourceParam.get("param_value"));
				}
			}
			source.put("connect_info", json);
		}
		
		Map<String,Object> data = new HashMap<String,Object>();
		data.put("total_records", totalRecords);
		data.put("source_data", sourceList);
		
		return data;
	}

	/**
	 * 数据源存储类型
	 * @return
	 */
	public List<Map<String,Object>> getDataSourceType(){
		String sql = "select param_name as data_source_type_name,param_name as data_source_type from t_meta_public_param where param_key = 'data_source_type'";
		return dmJdbcTemplate.queryForList(sql);
		
	}
	
	/**
	 * 查询数据源明细信息
	 * @param id
	 * @return
	 */
	public Map<String,Object> queryDataSourceDetail(String id){
		
		String sql = "select id,data_source_code,data_source_name,data_source_type,'离线' as data_source_use,oper_user,data_source_desc,date_format(oper_time,'%Y-%m-%d %H:%i:%s') as oper_time from t_meta_data_source where id = ?";
		
		Map<String,Object> sourceMap = dmJdbcTemplate.queryForMap(sql,id);
		
		sql = "select id,param_key,param_value from t_meta_data_source_param where id = ?";
		List<Map<String, Object>> sourceParams = dmJdbcTemplate.queryForList(sql,id);
		JSONObject json = new JSONObject();
		for (Map<String, Object> sourceParam : sourceParams) {
			json.put(sourceParam.get("param_key"), sourceParam.get("param_value"));
		}
		sourceMap.put("connect_info", json);
		
		return sourceMap;
	}
	
	private String getDataSourceSql(String data_source_name,String keyword,String data_source_type) {
		StringBuffer sql = new StringBuffer();
		sql.append("select id,data_source_code,data_source_name,data_source_type,'离线' as data_source_use,oper_user,data_source_desc,date_format(oper_time,'%Y-%m-%d %H:%i:%s') as oper_time from t_meta_data_source where status = 0");
		if(StringUtils.isNotBlank(data_source_name)){
			sql.append(" and data_source_name like ?");
		}
		if(StringUtils.isNotBlank(keyword)){
			sql.append(" and (data_source_code like ? or data_source_name like ?)");
		}
		if(StringUtils.isNotBlank(data_source_type)){
			sql.append(" and data_source_type = ?");
		}
		sql.append("  order by oper_time desc");
		sql.append(" limit ? offset ? ");
		return sql.toString();
	}
	
	private String getDataSourceCountSql(List<Object> paramList,String data_source_name,String keyword,String data_source_type) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(1) count from t_meta_data_source where status = 0");
		if(StringUtils.isNotBlank(data_source_name)){
			sql.append(" and data_source_name like ?");
			paramList.add("%"+data_source_name+"%");
		}
		if(StringUtils.isNotBlank(keyword)){
			sql.append(" and (data_source_code like ? or data_source_name like ?)");
			paramList.add("%"+keyword+"%");
			paramList.add("%"+keyword+"%");
		}
		if(StringUtils.isNotBlank(data_source_type)){
			sql.append(" and data_source_type = ?");
			paramList.add(data_source_type);
		}
		return sql.toString();
	}
}